#!/usr/bin/python
# Filename: tag.py

def max_dict(s):
	value = float('-inf');
	key = '';
	for name in s:
		if s[name] >= value:
			key = name;
			value = s[name];
	return (key,value);

q_tbl = dict();
tags = set();
e_tbl = dict();

fr_e_dat = file('emission_rep.dat2');
for line in fr_e_dat:    #read e(x|y) into e_tbl and add all the tags into tags
	words = line.strip().split(' ');
	if words[0] in e_tbl:
		e_tbl[words[0]][words[1]] = float(words[2]);
	else:
		e_tbl[words[0]] = {words[1] : float(words[2])}; 
	if not(tags.__contains__(words[1])):
			tags.add(words[1]);
fr_e_dat.close();

print e_tbl;

fr_dev = file('ner_dev.dat');
fw_dev = open('ner_dev2.tag','w');

key = '';
value = 0;
for line in fr_dev:
	lines = '';
	words = line.strip().split(' ');
	print words;
	if(words[0] != ''):
		if words[0] in e_tbl:
			(key,value) = max_dict(e_tbl[words[0]]);
		else:
			(key,value) = max_dict(e_tbl['_RARE_']);
		print key,value;
		lines = words[0] + ' ' + key + ' '+ str(value)+'\n';
	else:
		lines = '\n';
	
	fw_dev.write(lines);
fw_dev.close();
fr_dev.close();
		
